package com.thinkfit.jy;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.DisplayMetrics;

import androidx.multidex.MultiDexApplication;

import com.clj.fastble.BleManager;
import com.common.lib.manager.ConfigurationManager;
import com.common.lib.manager.DataManager;
import com.common.lib.network.HttpMethods;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.thinkfit.jy.activity.ApplicationActivity;
import com.thinkfit.jy.activity.FreeMovementNewActivity;
import com.thinkfit.jy.activity.MainActivity;
import com.thinkfit.jy.fragment.ApplicationFragment;
import com.thinkfit.jy.serialport.SportType;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.MotionDataUtils;
import com.thinkfit.jy.utils.ShowBackButtonViewUtils;
import com.thinkfit.jy.utils.ShowSpeedViewUtils;
import com.thinkfit.jy.utils.SportsDataUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;

public class BaseApplication extends MultiDexApplication {

    private static BaseApplication mApplicationInstance;
    private ActivityLifecycleCallbacksImpl mActivityLifecycleCallbacksImpl;
    private static int activityCounter=0;

    public static BaseApplication getInstance() {
        if (null == mApplicationInstance) {
            mApplicationInstance = new BaseApplication();
        }
        return mApplicationInstance;
    }

    static {//static 代码段可以防止内存泄露
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(
                        DataManager.Companion.getInstance().getMainStyle() == 3 ? com.common.lib.R.color.color_33_ff_ff_ff :
                                com.common.lib.R.color.color_00_00_00, com.common.lib.R.color.text_color_1);//全局设置主题颜色
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });

        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
    }


    @Override
    public void onCreate() {
        super.onCreate();

        mActivityLifecycleCallbacksImpl = new ActivityLifecycleCallbacksImpl();
        this.registerActivityLifecycleCallbacks(mActivityLifecycleCallbacksImpl);

        ConfigurationManager.Companion.getInstance().setContext(this);

//        HttpMethods.Companion.getInstance().setSType(SerialPortManager.mCurrentDeviceType == DeviceType.CAR_WATCH ?
//                "1" : "2");
        HttpMethods.Companion.getInstance().setSType(DeviceTypeUtils.getDeviceType()+"");
        //     SerialPortManager.getInstance();
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();

        BleManager.getInstance().init(this);//初始化蓝牙
        BleManager.getInstance()
                .enableLog(true)
                .setReConnectCount(1, 5000)//设置重连次数和间隔时间  重连次数1，间隔5000毫秒
                .setSplitWriteNum(20) //设置写入的拆分字数
                .setConnectOverTime(10000)//设置连接时间
                .setOperateTimeout(5000);//设置操作超时时间

    }

    boolean isShowSport = true;



    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    private class ActivityLifecycleCallbacksImpl implements ActivityLifecycleCallbacks{
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            System.out.println("onActivityCreated --> "+activity.getClass().getName());

        }

        @Override
        public void onActivityStarted(Activity activity) {
            activityCounter++;
            System.out.println("onActivityStarted --> "+activity.getClass().getName()+",activityCounter="+activityCounter);
            if (activityCounter>0 && !isShowSport){
                isShowSport = true;
                ShowBackButtonViewUtils.getInstance().hideBackButtonView();
                if(activity.getClass().getName().equals(ApplicationFragment.class.getName()) || activity.getClass().getName().equals(MainActivity.class.getName())){
                    if(MotionDataUtils.getInstance().isRun()||MotionDataUtils.getInstance().isPause()){
                        if(SportsDataUtils.getInstance().mSportType == SportType.FREE){
                            Intent intent = new Intent(activity, FreeMovementNewActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            activity.startActivity(intent);
                        } else if(SportsDataUtils.getInstance().mSportType == SportType.VIDEO){

                        }else if(SportsDataUtils.getInstance().mSportType == SportType.LIVE){

                        }
                    }
                }
            }
        }

        @Override
        public void onActivityResumed(Activity activity) {
            System.out.println("onActivityResumed --> "+activity.getClass().getName());
        }

        @Override
        public void onActivityPaused(Activity activity) {
            System.out.println("onActivityPaused --> "+activity.getClass().getName());
        }

        @Override
        public void onActivityStopped(Activity activity) {
            activityCounter--;
            System.out.println("onActivityStopped --> "+activity.getClass().getName()+",activityCounter="+activityCounter);
            if (activityCounter==0){
                isShowSport = false;
                ShowBackButtonViewUtils.getInstance().ShowBackButtonView(activity);
                SportsStatusBarViewUtils.getInstance().initView(activity);
//                SportsStatusBarViewUtils.getInstance().atThirdParty(true);
                SportsStatusBarViewUtils.getInstance().isMain(true);
                ShowSpeedViewUtils.getInstance().initView(activity);
            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            System.out.println("onActivitySaveInstanceState --> "+activity.getClass().getName());
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            System.out.println("onActivityDestroyed --> "+activity.getClass().getName());
        }

    }
}
